<?php 

namespace app\index\controller;
use think\Controller;
use think\Db;

//use alidayu\top\TopClient;
//use alidayu\top\request\AlibabaAliqinFcSmsNumSendRequest;

//require_once $_SERVER['DOCUMENT_ROOT'].'/extend/dayu2.0/vendor/autoload.php';
 

// use Aliyun\Core\Config;
// use Aliyun\Core\Profile\DefaultProfile;
// use Aliyun\Core\DefaultAcsClient;
// use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;
// use Aliyun\Api\Sms\Request\V20170525\QuerySendDetailsRequest;

// 加载区域结点配置
//Config::load();

class Msm extends Controller{

	public function testsend()
	{
		$code = 458645;
		$res = $this->sendsms(2175, $code ,18822878177);
		dump($res);
	}

	public function sendsm($uid = 0,$phone)
	{ 

		if(!$phone){
			return false;
		}
		$clapi  = new \ChuanglanSmsApi();
		$code = mt_rand(100000,999999);

		//设置您要发送的内容：其中“【】”中括号为运营商签名符号，多签名内容前置添加提交
		$result = $clapi->sendSMS($phone,'【期权赢】您好，您的验证码'.$code );

		if(!is_null(json_decode($result))){
			
			$output=json_decode($result,true);

			if(isset($output['code'])  && $output['code']=='0'){
				//echo '发送成功';
				return true;
			}else{
				//echo $output['errorMsg'];
				return false;
			}

		}else{
				echo $result; 
		}
	}



	/**
	 * 短信宝 http://www.smsbao.com/***/
	//--------------------------------------------------------------
	// public function sendsms($uid = 0, $code ,$phone)
	// {
	// 	$conf = getconf('');

	// 	if(!$code){
	// 		return false;
	// 	}

	// 	if(!$phone){
	// 		return false;
	// 	}

	// 	$content = '您的验证码为'.$code.'，在10分钟内有效。';
		
	// 	$smsapi = "http://zz.253.com/"; //短信网关
	// 	$user = "18053164231"; //短信平台帐号
	// 	$pass = md5("BFMq2,b244"); //短信平台密码
	// 	$content="【".$conf['msm_SignName']."】".$content;//要发送的短信内容
	// 	$phone = $phone;
	// 	$sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
		
	// 	$result =file_get_contents($sendurl) ;
	// 	if($result != 0){
	// 		return false;
	// 	}else{
	// 		return true;
	// 	}

	// } 
//-------------------------------------------------------
	
	//public function sendsms($uid = 0, $code ,$phone){
	//	$conf = getconf('');


	//	$url="https://api.dingdongcloud.com/v1/sms/sendyzm";
 //       $data = "apikey=%s&mobile=%s&content=%s";
 //       $content = "【".$conf['msm_SignName']."】你的验证码是".$code."，请在10分钟内输入。";
 //       $content = urlencode($content);
 //       $apikey = $conf['msm_appkey'];
 //       $mobile = $phone; 
        
 //       $rdata = sprintf($data, $apikey, $mobile, $content);
 //       $url = $url.'?'.$rdata;
       	
 //      	$api = controller('Api');
 //      	$result = $api->curlfun($url);
 //      	$arr = json_decode($result,1);
 //       if($arr['code'] == 1){
 //       	return true;
 //       }else{
 //       	return false;
 //       }


        
	//}
	
	

	
/*
	public function sendsms($uid = 0, $code ,$phone){
		$conf = getconf('');
		// 初始化SendSmsRequest实例用于设置发送短信的参数
        $request = new SendSmsRequest();

        // 必填，设置雉短信接收号码
        $request->setPhoneNumbers($phone);

        // 必填，设置签名名称
        $request->setSignName($conf['msm_SignName']);

        // 必填，设置模板CODE
        $request->setTemplateCode($conf['msm_TCode']);

        // 可选，设置模板参数
        $templateParam = Array( "code"=>$code);

        if($templateParam) {
            $request->setTemplateParam(json_encode($templateParam));
        }

        // 暂时不支持多Region
        $region = "cn-hangzhou";
        // 服务结点
        $endPointName = "cn-hangzhou";
        // 短信API产品名
        $product = "Dysmsapi";
        // 短信API产品域名
        $domain = "dysmsapi.aliyuncs.com";
        // 初始化用户Profile实例
        $profile = DefaultProfile::getProfile($region, $conf['msm_appkey'], $conf['msm_secretkey']);

        // 增加服务结点
        DefaultProfile::addEndpoint($endPointName, $region, $product, $domain);

        $this->acsClient = new DefaultAcsClient($profile);
        // 发起访问请求
        $acsResponse = $this->acsClient->getAcsResponse($request);

        // 打印请求结果
        // var_dump($acsResponse);
        $array = json_decode(json_encode($acsResponse),TRUE);
        
        if(isset($array['Code']) && $array['Code'] == "OK"){
			return true;
		}else{
			return false;
		}
	}

	/*
	public function sendsms($uid = 0, $code ,$phone)
	{
		$conf = getconf('');
		$c = new TopClient();
		$c ->appkey = trim($conf['msm_appkey']) ;
		$c ->secretKey = trim($conf['msm_secretkey']) ;
		$req = new AlibabaAliqinFcSmsNumSendRequest;
		$req ->setExtend( $uid );
		$req ->setSmsType( "normal" );
		$req ->setSmsFreeSignName( trim($conf['msm_SignName']) );
		$req ->setSmsParam("{\"code\":\"$code\"}");
		$req ->setRecNum( trim($phone) );
		$req ->setSmsTemplateCode( trim($conf['msm_TCode']) );
		
		

		$resp = $c ->execute( $req );
		$array = json_decode(json_encode($resp),TRUE);
		dump($array);
		if(isset($array['result']["success"]) && $array['result']["success"] == "true"){
			return true;
		}else{
			return false;
		}
		
	}

	*/




}

